package com.lebang.controller.sys;

import com.lebang.common.Login;
import com.lebang.model.statistics.RecordModel;
import com.lebang.model.statistics.StatisticsMember;
import com.lebang.model.statistics.StatisticsRecord;
import com.lebang.service.PointWallSdkService;
import com.lebang.service.StatisticsService;
import com.lebang.service.buss.MemberRecordService;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 统计数据
 *
 * @Author: hwq
 * @Date: 2019/9/16 10:00
 * @Version 1.0
 */
@RequestMapping("/statistics")
@Controller
public class StatisticsController {
    @Autowired
    private StatisticsService statisticsService;
    @Autowired
    private MemberRecordService recordService;
    @Autowired
    private PointWallSdkService pointWallSdkService;

    /**
     * 用户统计页面跳转
     *
     * @return
     */
    @GetMapping("/member/statistics")
    @Login
    public String statistics() {
        return "/statistics/member";
    }


    @PostMapping("/member/statistics")
    @Login
    @ResponseBody
    public Map<String, Object> statistics(HttpServletRequest request,
                                          String starTime, String endTime, int type) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime star;
        LocalDateTime end;
        if (TextUtil.isEmpty(starTime, endTime)) {
            star = null;
            end = null;
        } else {
            star = LocalDateTime.parse(starTime, df);
            end = LocalDateTime.parse(endTime, df);
        }

        //获取渠道用户总量appStore以及count
        List<StatisticsMember> statisticsMembers = statisticsService.selectAllAppStoreMemberCount(star, end, type);
        statisticsMembers.forEach(vo -> {
            int zySum = recordService.sumMemberTradeByTime(vo.getAppStore(), star, end, type);
            //任务记录的单位为分

            vo.setZyMoney(new BigDecimal(zySum).divide(new BigDecimal(100)));
            //渠道单位为元
            BigDecimal brandSum = pointWallSdkService.sumPointWallByTime(vo.getAppStore(), star, end, type);
            vo.setBrandMoney(brandSum);


            BigDecimal xianwan = pointWallSdkService.sumXianwanByTime(vo.getAppStore(), star, end, type);
            vo.setBrandMoney(vo.getBrandMoney().add(xianwan));


            BigDecimal aotian = pointWallSdkService.sumAotianByTime(vo.getAppStore(), star, end, type).multiply(new BigDecimal(2));
            vo.setBrandMoney(vo.getBrandMoney().add(aotian));


            int usermoney = pointWallSdkService.sumUserByTime(vo.getAppStore(), star, end, type);
            vo.setUserMoney(new BigDecimal(usermoney).divide(new BigDecimal(100)));
            //已提现
            int tixian = pointWallSdkService.sumUserOutLay(vo.getAppStore(), star, end, type);
            vo.setUserOutLay(new BigDecimal(tixian).divide(new BigDecimal(100)));
            vo.setMyMoney(vo.getZyMoney().add(vo.getBrandMoney()));
        });
        Map<String, Object> map = new HashMap<>(1);
        map.put("data", statisticsMembers);
        map.put("count", 10);
        map.put("code", 0);
        return map;
    }

    /**
     * 用户统计页面跳转
     *
     * @return
     */
    @GetMapping("/record/statistics")
    @Login
    public String record() {
        return "/statistics/record";
    }


    @PostMapping("/record/statistics")
    @Login
    @ResponseBody
    public Map<String, Object> record(HttpServletRequest request,
                                      String starTime, String endTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime star;
        LocalDateTime end;
        if (TextUtil.isEmpty(starTime, endTime)) {
            star = null;
            end = null;
        } else {
            star = LocalDateTime.parse(starTime, df);
            end = LocalDateTime.parse(endTime, df);
        }
        //获取渠道用户总量appStore以及count
        List<StatisticsRecord> statisticsMembers = recordService.sumRecordByTime(star, end);
        List<RecordModel> recordModels = recordService.getAllTitleList();
        Map<String, String> map = new HashMap<>(1);
        recordModels.forEach(vo -> map.put(vo.getTaskId(), vo.getTitle()));
        statisticsMembers.forEach(s -> s.setTitle(map.get(s.getTaskId())));
        statisticsMembers = statisticsMembers.stream().filter(vo -> !TextUtil.isEmpty(vo.getTitle())).collect(Collectors.toList());
        HashMap<String, Object> result = new HashMap<>(1);
        result.put("data", statisticsMembers);
        result.put("count", statisticsMembers.size());
        result.put("code", 0);
        return result;
    }
}
